import {
  Component,
  EventEmitter,
  Input,
  OnChanges,
  OnInit,
  Output,
  SimpleChanges,
} from '@angular/core';
import clone from 'lodash.clonedeep';
@Component({
  selector: 'app-transfer',
  templateUrl: './transfer.component.html',
  styleUrls: ['./transfer.component.scss']
})
export class TransferComponent implements OnInit,OnChanges{
  @Input() public DataSource = [];
  isdisable = true;
  isdisable2 = true;
  ischecked = false;
  ischecked2 = false;
  @Output() changeEvent : EventEmitter<any> = new EventEmitter();
  list = [];
  leftDataLen = 0;
  leftDataLenAll: any;
  rightDataLen = 0;
  rightDataLenAll: any;
  leftList = [];
  rightList = [];
  searchLeft ='';
  searchRight ='';
  constructor() {
  }
  //搜索框
  lowQuery(isLeft){
    if(isLeft){
      if (this.searchLeft == "") {
        this.formatList()
      } else {
        this.leftList = [...this.leftList.filter(x => x.value.includes(this.searchLeft))];
        this.leftDataLenAll = this.leftList.length;
      }
    }else{
      if (this.searchRight == "") {
        this.formatList()
      } else {
        this.rightList = [...this.rightList.filter(x => x.value.includes(this.searchRight))];
        this.rightDataLenAll = this.rightList.length;
      }
    }
  }
  ngOnChanges(changes: SimpleChanges): void {
    if ('DataSource' in changes) {
      this.list = clone(this.DataSource);
      this.formatList();
    }

  }
  ngOnInit() {
    // this.list = clone(this.DataSource);
    // this.list = this.DataSource;
    // this.formatList();
  }

  formatList(){
    this.leftList = [...this.list.filter(x => x._left)];
    this.rightList =[...this.list.filter(x => x._right)];
    this.rightDataLenAll =   this.rightList.length;
    this.leftDataLenAll =  this.leftList.length;
    this.changeEvent.emit(this.list)
  }


  checkChange(isLeft) {
    if(isLeft){
      this.leftDataLen = 0;
      this.ischecked = false;
      this.isdisable2  = true;
      this.leftList.filter(x=>x.checked).forEach(x=>{
        x._right = true;
        x._left = false;
        x.checked = false
      })

    }else if(!isLeft){
      this.rightDataLen = 0;
      this.ischecked2 = false;
      this.isdisable  = true;
      this.rightList.filter(x=>x.checked).forEach(x=>{
        x._right = false;
        x._left = true;
        x.checked = false
      })
    }
    this.formatList()
  }
  onCheck($event,item,isLeft){
    if (isLeft) {
      if ($event == true) {
        this.leftList.filter(x => item.key == x.key).forEach(x => {
          x.checked = true;
        })
      } else {
        this.leftList.filter(x => item.key == x.key).forEach(x => {
          x.checked = false;
        })
      }
      let i = 0;
      this.leftList.filter(x => x.checked).forEach(x => {
        i = i + 1;
      });
      this.leftDataLen = i;
      if (this.leftDataLen == 0) {
        this.isdisable2 = true;
      } else {
        this.isdisable2 = false;
      }
    } else{
      if ($event == true) {
        this.rightList.filter(x => item.key == x.key).forEach(x => {
          x.checked = true;
        })
      } else {
        this.rightList.filter(x => item.key == x.key).forEach(x => {
          x.checked = false;
        })
      }
      let i = 0;
      this.rightList.filter(x => x.checked).forEach(x => {
        i = i + 1;
      });
      this.rightDataLen = i;
      if (this.rightDataLen == 0) {
        this.isdisable = true;
      } else {
        this.isdisable = false;
      }
    }
  }

  onCheckChange($event,isLeft){
    if(isLeft){
      if($event==true){
        let i=0;
        this.leftList.filter(x=>!x.disabled).forEach(x=>{
          i=i+1;
          x.checked = true;
        });
        this.leftDataLen =i;
        if(this.leftDataLen >0){
          this.isdisable2 = false;
        }
      }else if($event==false){
        this.leftList.filter(x=>!x.disabled).forEach(x=>{
          x.checked = false;
        });
        this.leftDataLen = 0;
        this.isdisable2 = true;
      }
    }else{
      if($event==true){
        let i=0;
        this.rightList.filter(x=>!x.disabled).forEach(x=>{
          i=i+1;
          x.checked = true;
        })
        this.rightDataLen =i;
        if(this.rightDataLen>0){
          this.isdisable = false;
        }
      }else if($event==false){
        this.rightList.filter(x=>!x.disabled).forEach(x=>{
          x.checked = false;
        })
        this.rightDataLen = 0;
        this.isdisable = true;
      }
    }

  }

}
